from fastapi import APIRouter, Depends, Query
from sqlalchemy.orm import Session

from common.database import get_db
from core.response import resp_200, resp_400
from core.logger import logger


from app.manage_notice.handlers.manage_notice_user_handler import ManageNoticeUserHandler
from app.manage_notice.handlers.manage_notice_team_handler import ManageNoticeTeamHandler
from app.manage_notice.handlers.manage_notice_department_handler import ManageNoticeDepartmentHandler
from core.security import permission_checker

from app.manage_notice.schemas import *


router = APIRouter()




#### 通知用户操作 ####
@router.get('/user/', summary="根据表字段-模糊匹配, 获取通知人信息", description="统一管理-通知")
async def get_manage_notice_user_search(field: str = Query("all"), value: str = Query(""), page: int = Query(1, ge=1), per_page: int = Query(10, ge=0),
        db: Session = Depends(get_db), user=Depends(permission_checker)):
    manage_notice_user_handler = ManageNoticeUserHandler()
    status, result = manage_notice_user_handler.get_notice_user_search(db, page, per_page, field, value)
    if not status:
        message = "get notice user search failed"
        logger.error(f"{message} => page:{page} per_page:{per_page} field:{field} value:{value}")
        return resp_400(message)
    return resp_200(result)


@router.get('/user/match/', summary="根据表字段-完全匹配, 获取通知人信息", description="统一管理-通知")
async def get_manage_notice_user_match(field: str = Query(..., description="需要查询的字段"), value: str = Query(..., description="字段的值"),
                                page: int = Query(1, ge=1), per_page: int = Query(10, ge=0),
                                db: Session = Depends(get_db), user=Depends(permission_checker)):
    manage_notice_user_handler = ManageNoticeUserHandler()
    result = manage_notice_user_handler.get_notice_user_match(db, page, per_page, field, value)
    if not result:
        message = "get notice user match failed"
        logger.error(f"{message} => page:{page} per_page:{per_page} field:{field} value:{value}")
        return resp_400(message)
    return resp_200(result)


@router.post('/user/', summary="创建通知人", description="统一管理-通知")
async def post_manage_notice_user(data: PostNoticeUser, db: Session = Depends(get_db), user=Depends(permission_checker)):
    manage_notice_user_handler = ManageNoticeUserHandler()
    result = manage_notice_user_handler.add_notice_user(db, data.dict())
    if not result:
        message = "post notice user failed"
        logger.error(f"{message} => data:{data.dict()}")
        return resp_400(message)
    return resp_200()


@router.put('/user/', summary="修改通知人信息", description="统一管理-通知")
async def put_manage_notice_user(data: PutNoticeUser, db: Session = Depends(get_db), user=Depends(permission_checker)):
    manage_notice_user_handler = ManageNoticeUserHandler()
    result = manage_notice_user_handler.update_notice_user(db, data.dict())
    if not result:
        message = "put notice user failed"
        logger.error(f"{message} => data:{data.dict()}")
        return resp_400(message)
    return resp_200()


@router.delete('/user/', summary="删除通知人信息", description="统一管理-通知")
async def delete_manage_notice_user(data: DelBaseModel, db: Session = Depends(get_db), user=Depends(permission_checker)):
    manage_notice_user_handler = ManageNoticeUserHandler()
    result = manage_notice_user_handler.delete_notice_user(db, data.dict())
    if not result:
        message = "delete notice user failed"
        logger.error(f"{message} => data:{data}")
        return resp_400(message)
    return resp_200()






#### 通知用户组操作 ####
@router.get('/team/', summary="根据表字段-模糊匹配, 获取通知组信息", description="统一管理-通知")
async def get_manage_notice_team_search(field: str = Query("all"), value: str = Query(""), page: int = Query(1, ge=1), per_page: int = Query(10, ge=0),
        db: Session = Depends(get_db), user=Depends(permission_checker)):
    manage_notice_team_handler = ManageNoticeTeamHandler()
    status, result = manage_notice_team_handler.get_notice_team_search(db, page, per_page, field, value)
    if not status:
        message = "get notice team search failed"
        logger.error(f"{message} => page:{page} per_page:{per_page} field:{field} value:{value}")
        return resp_400(message)
    return resp_200(result)


@router.get('/team/match/', summary="根据表字段-完全匹配, 获取通知组信息", description="统一管理-通知")
async def get_manage_notice_team_match(field: str = Query(..., description="需要查询的字段"), value: str = Query(..., description="字段的值"),
                                page: int = Query(1, ge=1), per_page: int = Query(10, ge=0),
                                db: Session = Depends(get_db), user=Depends(permission_checker)):
    manage_notice_team_handler = ManageNoticeTeamHandler()
    result = manage_notice_team_handler.get_notice_team_match(db, page, per_page, field, value)
    if not result:
        message = "get notice team match failed"
        logger.error(f"{message} => page:{page} per_page:{per_page} field:{field} value:{value}")
        return resp_400(message)
    return resp_200(result)


@router.post('/team/', summary="创建通知组", description="统一管理-通知")
async def post_manage_notice_team(data: PostNoticeTeam, db: Session = Depends(get_db), user=Depends(permission_checker)):
    manage_notice_team_handler = ManageNoticeTeamHandler()
    result = manage_notice_team_handler.add_notice_team(db, data.dict())
    if not result:
        message = "post notice team failed"
        logger.error(f"{message} => data:{data.dict()}")
        return resp_400(message)
    return resp_200()


@router.put('/team/', summary="修改通知组信息", description="统一管理-通知")
async def put_manage_notice_team(data: PutNoticeTeam, db: Session = Depends(get_db), user=Depends(permission_checker)):
    manage_notice_team_handler = ManageNoticeTeamHandler()
    result = manage_notice_team_handler.update_notice_team(db, data.dict())
    if not result:
        message = "put notice team failed"
        logger.error(f"{message} => data:{data.dict()}")
        return resp_400(message)
    return resp_200()


@router.delete('/team/', summary="删除通知组信息", description="统一管理-通知")
async def delete_manage_notice_team(data: DelBaseModel, db: Session = Depends(get_db), user=Depends(permission_checker)):
    manage_notice_team_handler = ManageNoticeTeamHandler()
    result = manage_notice_team_handler.delete_notice_team(db, data.dict())
    if not result:
        message = "delete notice team failed"
        logger.error(f"{message} => data:{data}")
        return resp_400(message)
    return resp_200()





#### 部门操作 ####
@router.get('/department/', summary="获取部门信息, 根据表字段-模糊匹配", description="统一管理-通知")
async def get_manage_notice_department_search(field: str = Query("all"), value: str = Query(""), page: int = Query(1, ge=1), per_page: int = Query(10, ge=0),
        db: Session = Depends(get_db), user=Depends(permission_checker)):
    manage_notice_department_handler = ManageNoticeDepartmentHandler()
    status, result = manage_notice_department_handler.get_department_search(db, page, per_page, field, value)
    if not status:
        message = "get department info search failed"
        logger.error(f"{message} => page:{page} per_page:{per_page} field:{field} value:{value}")
        return resp_400(message)
    return resp_200(result)


@router.get('/department/match/', summary="根据表字段-完全匹配, 获取部门信息", description="统一管理-通知")
async def get_manage_notice_department_match(field: str = Query(..., description="需要查询的字段"), value: str = Query(..., description="字段的值"),
                                    page: int = Query(1, ge=1), per_page: int = Query(10, ge=0),
                                    db: Session = Depends(get_db), user=Depends(permission_checker)):
    manage_notice_department_handler = ManageNoticeDepartmentHandler()
    result = manage_notice_department_handler.get_department_match(db, page, per_page, field, value)
    if not result:
        message = "get department info match failed"
        logger.error(f"{message} => page:{page} per_page:{per_page} field:{field} value:{value}")
        return resp_400(message)
    return resp_200(result)


@router.post('/department/', summary="增加部门信息", description="统一管理-通知")
async def post_manage_notice_department(data: PostDepartmentInfo, db: Session = Depends(get_db), user=Depends(permission_checker)):
    manage_notice_department_handler = ManageNoticeDepartmentHandler()
    result = manage_notice_department_handler.add_department(db, data.dict())
    if not result:
        message = "post department info failed"
        logger.error(f"{message} => data:{data.dict()}")
        return resp_400(message)
    return resp_200()


@router.put('/department/', summary="修改部门信息", description="统一管理-通知")
async def put_manage_notice_department(data: PutDepartmentInfo, db: Session = Depends(get_db), user=Depends(permission_checker)):
    manage_notice_department_handler = ManageNoticeDepartmentHandler()
    result = manage_notice_department_handler.update_department(db, data.dict())
    if not result:
        message = "put department info failed"
        logger.error(f"{message} => data:{data.dict()}")
        return resp_400(message)
    return resp_200()


@router.delete('/department/', summary="删除部门信息", description="统一管理-通知")
async def delete_manage_notice_department(data: DelBaseModel, db: Session = Depends(get_db), user=Depends(permission_checker)):
    manage_notice_department_handler = ManageNoticeDepartmentHandler()
    result = manage_notice_department_handler.delete_department(db, data.dict())
    if not result:
        message = "delete department info failed"
        logger.error(f"{message} => data:{data.dict()}")
        return resp_400(message)
    return resp_200()

